package com.example.ui;

import org.json.JSONException;
import org.json.JSONObject;

import com.example.huashangapp.HSApplication;
import com.example.huashangapp.R;
import com.example.net.NetRequest;
import com.example.service.MainService;
import com.example.ui.other.AboutOurActivity;

import com.example.ui.register.RegisterActivity01;
import com.example.ui.register.RegisterFragment;
import com.example.util.AsyncLoadImage;
import com.example.util.Util;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Layout;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;

public class IndexActivity extends Activity {
	private Button b_login, b_register;
	private ImageView helpImg;
	private static ImageView centerImg;
	private RelativeLayout relation;
	public static boolean isLogin = false;
	public static boolean isRegister = false;
    AsyncLoadImage asyncLoadImage;
   
    String data;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.a_index);
		Intent intent=getIntent();
		asyncLoadImage=new AsyncLoadImage();
		init();  
		uploadImg();
	}
	private String url;
	private SharedPreferences sp;
	private   void uploadImg() {
		centerImg.setTag(HSApplication.url);
		if(HSApplication.url!=null&&!"".equals(HSApplication.url)){
		asyncLoadImage.imageLoader.displayImage(HSApplication.url, centerImg,asyncLoadImage.options);
		centerImg.setTag(HSApplication.url);
		//Util.toast(this,"背景图"+url);
		}else{
			centerImg.setBackgroundResource(R.drawable.people);
		}
	};
		
			
	
	private void init() {
		//relation=(RelativeLayout)findViewById(R.id.al_betreew);
		centerImg=(ImageView) findViewById(R.id.al_bg);
		sp=HSApplication.sp;
		url=sp.getString("backUrl", "");
		helpImg = (ImageView) findViewById(R.id.al_help);
		b_login = (Button) findViewById(R.id.al_login);
		b_register = (Button) findViewById(R.id.al_register);
		b_login.setOnClickListener(la_ocl);
		b_register.setOnClickListener(la_ocl);  

		helpImg.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivity(new Intent(IndexActivity.this,
						AboutOurActivity.class));
			}
		});
	}

	OnClickListener la_ocl = new OnClickListener() {

		@Override
		public void onClick(View v) {
		
			switch (v.getId()) {
			case R.id.al_login:
				Intent i_l = new Intent();
				i_l.setClass(IndexActivity.this, LoginActivity.class);
				startActivity(i_l);
				finish();
				break;
			case R.id.al_register:
				Intent i_r = new Intent();
				i_r.setClass(IndexActivity.this, RegisterActivity01.class);
				startActivity(i_r);
				finish();
				break;
			default:
				break;
			}
		}
	};

	@Override
	protected void onRestart() {
		if (isLogin == true || isRegister == true) {
			finish();
		}
		super.onRestart();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode==4){
			try {
				Intent mainService = new Intent(this, MainService.class);
				stopService(mainService);
				finish();
			} catch (Exception e) {
				finish();
				System.exit(0);
				e.printStackTrace();
			}
		}
		return super.onKeyDown(keyCode, event);
	}

}
